package com.hmall.order.web;

import com.hmall.client.UserClient;
import com.hmall.common.dto.Address;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.RequestParams;
import com.hmall.order.service.IOrderService;
import com.hmall.order.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("order")
@Slf4j
public class OrderController {

   @Autowired
   private IOrderService orderService;

   @Autowired
   private UserClient userClient;

   @GetMapping("{id}")
   public Order queryOrderById(@PathVariable("id") Long orderId) {
      log.info("[orderController拦截到了用户id]: {}", UserHolder.getUser());

      Address address = userClient.findAddressById(60L);
      log.info("[远程调用用户地址信息]: {}", address);

      return orderService.getById(orderId);
   }


   @PostMapping
   public Order createOrder(@RequestBody RequestParams params) {
      // 创建订单，获取订单id
      Long orderId = orderService.createOrder(params);
      Order order = new Order();
      order.setId(orderId);
      // 封装返回结果
      return order;
   }
}